package com.cardone.platform.authority.controller;

import java.util.List;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.authority.dto.PermissionOrgDto;
import com.cardone.platform.authority.service.PermissionOrgService;

/**
 * 许可与组织
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/authority/permissionOrg/")
@Controller
public class PermissionOrgController {
	/**
	 * 初始化数据绑定
	 *
	 * @param webDataBinder
	 *          数据绑定
	 */
	// @InitBinder
	// @Deprecated
	// public void initBinder(final WebDataBinder webDataBinder) {
	// }

	/**
	 * 许可与组织：删除
	 *
	 * @param deletePermissionOrg
	 *          许可与组织对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Callable<ReturnDataDto<String>> deleteJson(final PermissionOrgDto deletePermissionOrg) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(PermissionOrgService.class).deleteByIds(deletePermissionOrg);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, PermissionOrgController.log);
				}
			}
		};
	}

	/**
	 * 许可与组织：分页查询
	 *
	 * @param findPermissionOrg
	 *          许可与组织对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
	public void editHtml(final PermissionOrgDto findPermissionOrg, final Model model) {
		final PermissionOrgDto permissionOrg = ContextHolder.getBean(PermissionOrgService.class).findById(PermissionOrgDto.class, findPermissionOrg);

		model.addAttribute("permissionOrg", permissionOrg);
	}

	/**
	 * 许可与组织：编辑
	 *
	 * @param findPermissionOrg
	 *          许可与组织对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<PermissionOrgDto>> findByIdJson(final PermissionOrgDto findPermissionOrg, final Model model) {
		return new Callable<ReturnDataDto<PermissionOrgDto>>() {
			@Override
			public ReturnDataDto<PermissionOrgDto> call() throws Exception {
				try {
					final PermissionOrgDto permissionOrg = ContextHolder.getBean(PermissionOrgService.class).findById(PermissionOrgDto.class, findPermissionOrg);

					return ReturnDataUtils.newReturnData(permissionOrg);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, PermissionOrgController.log);
				}
			}
		};
	}

	/**
	 * 许可与组织：查询
	 *
	 * @param findListPermissionOrg
	 *          许可与组织对象
	 * @return 许可与组织对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<List<PermissionOrgDto>>> findListByLikeCodeJson(final PermissionOrgDto findListPermissionOrg) {
		return new Callable<ReturnDataDto<List<PermissionOrgDto>>>() {
			@Override
			public ReturnDataDto<List<PermissionOrgDto>> call() throws Exception {
				try {
					final List<PermissionOrgDto> permissionOrgList = ContextHolder.getBean(PermissionOrgService.class).findListByLikeCode(PermissionOrgDto.class, findListPermissionOrg);

					return ReturnDataUtils.newReturnData(permissionOrgList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, PermissionOrgController.log);
				}
			}
		};
	}

	/**
	 * 许可与组织：分页查询
	 *
	 * @param paginationPermissionOrg
	 *          许可与组织对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<PaginationDto<PermissionOrgDto>>> paginationByLikeCodeJson(final PermissionOrgDto paginationPermissionOrg) {
		return new Callable<ReturnDataDto<PaginationDto<PermissionOrgDto>>>() {
			@Override
			public ReturnDataDto<PaginationDto<PermissionOrgDto>> call() throws Exception {
				try {
					final PaginationDto<PermissionOrgDto> permissionOrgPagination = ContextHolder.getBean(PermissionOrgService.class).paginationByLikeCode(PermissionOrgDto.class, paginationPermissionOrg);

					return ReturnDataUtils.newReturnData(permissionOrgPagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, PermissionOrgController.log);
				}
			}
		};
	}

	/**
	 * 许可与组织：分页查询
	 *
	 * @param paginationPermissionOrg
	 *          许可与组织对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
	public void paginationHtml(final PermissionOrgDto paginationPermissionOrg, final Model model) {
		final PaginationDto<PermissionOrgDto> permissionOrgPagination = ContextHolder.getBean(PermissionOrgService.class).paginationByLikeCode(PermissionOrgDto.class, paginationPermissionOrg);

		model.addAttribute("permissionOrgPagination", permissionOrgPagination);
	}

	/**
	 * 许可与组织：保存
	 *
	 * @param savePermissionOrg
	 *          许可与组织对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
	public Callable<ReturnDataDto<String>> saveJson(final PermissionOrgDto savePermissionOrg) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(PermissionOrgService.class).saveByIdOrCode(PermissionOrgDto.class, savePermissionOrg);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, PermissionOrgController.log);
				}
			}
		};
	}
}
